<?php

namespace Wx\Controller;

use Common\Controller\HomebaseController;
use Common\Model\MembersModel;

class WxauthController extends HomebaseController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $weixin = C('WEIXIN');
        $appid = $weixin['APPID'];
        $action = I('get.action');
        if ($action == "register") {
            $callbackUrl = 'http://' . $_SERVER['SERVER_NAME'] . '/wx/wxauth/callback_register';
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=" . $callbackUrl . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        } else if ($action == "login") {

            $callbackUrl = 'http://' . $_SERVER['SERVER_NAME'] . '/wx/wxauth/callback_login';
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=" . $callbackUrl . "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

        }
        header("location:$url");
    }

    public function callback_register()
    {
        $weixin = C('WEIXIN');
        $appid = $weixin['APPID'];
        $appSectet = $weixin['APPSECRET'];

        $code = I('get.code');
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appSectet&code=$code&grant_type=authorization_code";
        $res = http_request($url);
        $wx_open = json_decode($res, true);

        $wx_user = $this->getUserInfo($wx_open['access_token'],$wx_open['openid']);

//        $wx_user = array(
//            'openid' => '111111',
//            'nickname' => 'a.sun',
//            'headimgurl' => 'http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46'
//        );
        error_log("获取");
        error_log(print_r($wx_user,1));

        $this->setWxUser($wx_user['openid'], $wx_user);
        $this->redirect('/public/register');
    }


    public function callback_login()
    {
        $weixin = C('WEIXIN');
        $appid = $weixin['APPID'];
        $appSectet = $weixin['APPSECRET'];

        $code = I('get.code');
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appSectet&code=$code&grant_type=authorization_code";
        $res = http_request($url);
        $wx_open = json_decode($res, true);

        $wx_user = $this->getUserInfo($wx_open['access_token'],$wx_open['openid']);

//        $wx_user = array(
//            'openid' => '111111',
//            'nickname' => 'a.sun',
//            'headimgurl' => 'http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46'
//        );

        $memberModel = new MembersModel();

        $this->setWxUser($wx_user['openid'], $wx_user);
        $member = $memberModel->getMember($wx_user['openid']);
        $this->setMember($member['telephone'], $member);
        $this->redirect('/member/index');
    }



    private function getUserInfo($token,$openid){
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid&lang=zh_CN";
        $user_result = http_request($url);
        return json_decode($user_result, true);
    }
}


